package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import jdk.jshell.Snippet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 山顶洞人郑某
 * @className DishServiceImpl.java
 * @date 2024 01 20
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 添加菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            //添加口味数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page  =  dishMapper.page(dishPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    @Transactional
    /*
      菜品批量删除功能

      @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Integer status = dishMapper.getStatus(id);
            if (Objects.equals(status, StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIdsByDishIds !=null && setmealIdsByDishIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }

    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> list = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    /**
     * 修改菜品基础信息或者口味信息
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateById(dish);

        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        }
       dishFlavorMapper.insertBatch(flavors);
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void updateStatus(Integer status, Long id) {
        List<Long> list = new ArrayList<>();
        list.add(id);
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(list);
        if (setmealIdsByDishIds !=null && setmealIdsByDishIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateById(dish);
    }

    /**
     * 菜品分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {

        return dishMapper.list(categoryId);
    }

    /**
     * 根据套餐id查询套餐的菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getListByCategoryId(Long id) {
        List<DishItemVO> list = dishMapper.getListByCategoryId(id);
        return list;
    }

    /**
     * 根据套餐id查询菜品和口味信息
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> list = dishMapper.userList(dish);
        for (DishVO dishVO : list) {
            dishVO.setFlavors(dishFlavorMapper.getByDishId(dishVO.getId()));
        }
        return list;
    }

    /**
     * 查询起售和停售菜品数量信息
     *
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(dishMapper.getSoldAnddiscontinued(StatusConstant.ENABLE));
        dishOverViewVO.setDiscontinued(dishMapper.getSoldAnddiscontinued(StatusConstant.DISABLE));
        return dishOverViewVO;
    }
}
